
/**  
* @Title: PinyinUtil.java
* @Package com.utils.pinyin
* @Description: 
* @author likai
* @date 2016年12月12日
* @version V1.0  
*/

package com.utils.languge;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * @ClassName: PinyinUtil
 * @Description:
 * @author likai
 * @date 2016年12月12日
 *
 */

public class PinyinUtil {
	// 转换拼音不带音调
	public static String getChinesePinyin(String chinese) {
		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 无声调设置
		format.setVCharType(HanyuPinyinVCharType.WITH_V);
		format.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 转换后全小写输出
		String pinyin = "";
		try {
			pinyin = PinyinHelper.toHanYuPinyinString(chinese, format, "", true);
		} catch (BadHanyuPinyinOutputFormatCombination e) {
			e.printStackTrace();
		}
		return pinyin;
	}

	// 转换拼音带音调
	public static String getChinesePinyinAndTone(String chinese) {
		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
		// format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);//无声调设置
		format.setVCharType(HanyuPinyinVCharType.WITH_V);
		format.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 转换后全小写输出
		String pinyin = "";
		try {
			pinyin = PinyinHelper.toHanYuPinyinString(chinese, format, "", true);
		} catch (BadHanyuPinyinOutputFormatCombination e) {
			e.printStackTrace();
		}
		return pinyin;
	}

	
	
}
